package com.weixin.engine.config.time;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneOffset;

/**
 * 时间戳反序列化为LocalDateTime
 *
 * @author admin
 */
public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
    @Override
    public LocalDate deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {
        long timestamp = p.getValueAsLong();
        if (timestamp > 0) {
            return LocalDateTimeUtil.ofDate(LocalDateTimeUtil.of(timestamp, ZoneOffset.of("+8")));
        } else {
            LocalDate date = null;
            try {
                date = LocalDate.parse(p.getText());
            } catch (Exception e) {
                //ignore
            }
            return date;
        }
    }

//    @Override
//    public LocalDate deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {
//        long timestamp = p.getValueAsLong();
//        if (timestamp > 0) {
//            return LocalDateTimeUtil.ofDate(LocalDateTimeUtil.of(timestamp, ZoneOffset.of("+8")));
//        } else {
//            return null;
//        }
//    }
}